package com.example.hdwx.myapplication.base_activity;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.hdwx.myapplication.R;

public class WebActivity extends AppCompatActivity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        webView = (WebView) findViewById(R.id.WebView);
        webView.getSettings().setDomStorageEnabled(true);
        // 设置支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 启动缓存
        webView.getSettings().setAppCacheEnabled(true);
        // 设置缓存模式
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        //网页适配手机屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        init();
    }

    private void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(
                    WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                // handler.cancel();// Android默认的处理方式
                handler.proceed();// 接受所有网站的证书
                // handleMessage(Message msg);// 进行其他处理
            }
        });
        Intent intent = getIntent();
        //  int m= intent.getIntExtra("id",1);
        String n = intent.getStringExtra("url");
        Log.e("9999", "" + n + "");
        if (Integer.parseInt(n) > 10 && Integer.parseInt(n) < 100) {
            String url = "https://api.daydaycook.com.cn/daydaycook/server/activity/share.do?id=" + n;
            webView.loadUrl(url);
            Log.e("9999", "" + n + "" + url);
        } else if (Integer.parseInt(n) > 100000 && Integer.parseInt(n) < 1000000) {
            String url = "https://api.daydaycook.com.cn/daydaycook/website/details.html?id=" + n;
            webView.loadUrl(url);
            Log.e("9999", "" + n + "" + url);
        } else if (Integer.parseInt(n) > 10000 && Integer.parseInt(n) < 100000) {
            String url = "https://api.daydaycook.com.cn/daydaycook/h5/recipe/recipedetail.do?id=" + n;
            webView.loadUrl(url);
            Log.e("9999", "" + n + "" + url);
        }
    }

    @Override
    protected void onPause() {
        webView.reload();
        super.onPause();
    }
}
